/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanelWirelessRouter.java
 *
 * Created on 01/02/2012, 12:19:09
 */

package tsb.UI.Paneles.ROUTER;

import java.util.Random;
import tsb.dominio.enumeraciones.TiposSeguridad;
import tsb.dominio.excepciones.MACException;
import tsb.dominio.interfaces.IP;
import tsb.dominio.interfaces.MAC;
import tsb.dominio.interfaces.Wireless;

/**
 *
 * @author Nico
 */
public class PanelWirelessRouter extends javax.swing.JPanel {

    /** Creates new form PanelWirelessRouter */
    public PanelWirelessRouter() {
        initComponents();
        this.txtMAC.setText(MAC.generarMAC());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtIP = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtMascara = new javax.swing.JTextField();
        cmbEncendido = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();
        jRadioButton3 = new javax.swing.JRadioButton();
        txtMAC = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        Contraseña = new javax.swing.JPanel();
        rbtWEP = new javax.swing.JRadioButton();
        rbtWAP = new javax.swing.JRadioButton();
        jLabel2 = new javax.swing.JLabel();
        txtContraseña = new javax.swing.JTextField();
        chbSeguridad = new javax.swing.JCheckBox();

        jPanel2.setBackground(new java.awt.Color(255, 255, 255));
        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("IP"));
        jPanel2.setAlignmentX(0.0F);
        jPanel2.setAlignmentY(0.0F);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel5.setText("IP");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel4.setText("Mascara");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtMascara)
                    .addComponent(txtIP, javax.swing.GroupLayout.DEFAULT_SIZE, 280, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(txtIP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtMascara, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        cmbEncendido.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "    SI", "    NO" }));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel1.setText("Encendido:");

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Ancho de Banda"));
        jPanel1.setAlignmentX(0.3F);
        jPanel1.setAlignmentY(0.3F);

        jRadioButton1.setBackground(new java.awt.Color(255, 255, 255));
        jRadioButton1.setSelected(true);
        jRadioButton1.setText("11 Mbps");
        jRadioButton1.setEnabled(false);

        jRadioButton2.setBackground(new java.awt.Color(255, 255, 255));
        jRadioButton2.setText("54 Mbps");
        jRadioButton2.setEnabled(false);

        jRadioButton3.setBackground(new java.awt.Color(255, 255, 255));
        jRadioButton3.setText("270 Mbps");
        jRadioButton3.setEnabled(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jRadioButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jRadioButton3))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jRadioButton1)
                    .addComponent(jRadioButton2)
                    .addComponent(jRadioButton3))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel3.setText("MAC:");

        Contraseña.setBackground(new java.awt.Color(255, 255, 255));
        Contraseña.setBorder(javax.swing.BorderFactory.createTitledBorder("Seguridad"));
        Contraseña.setAlignmentX(0.0F);
        Contraseña.setAlignmentY(0.0F);
        Contraseña.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        rbtWEP.setBackground(new java.awt.Color(255, 255, 255));
        rbtWEP.setFont(new java.awt.Font("Tahoma", 0, 14));
        rbtWEP.setText("WEP");
        rbtWEP.setEnabled(false);

        rbtWAP.setBackground(new java.awt.Color(255, 255, 255));
        rbtWAP.setFont(new java.awt.Font("Tahoma", 0, 14));
        rbtWAP.setText("WAP");
        rbtWAP.setEnabled(false);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 14));
        jLabel2.setText("Contraseña:");

        txtContraseña.setEnabled(false);

        chbSeguridad.setBackground(new java.awt.Color(255, 255, 255));
        chbSeguridad.setFont(new java.awt.Font("Tahoma", 0, 14));
        chbSeguridad.setText("Activada");
        chbSeguridad.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                chbSeguridadMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout ContraseñaLayout = new javax.swing.GroupLayout(Contraseña);
        Contraseña.setLayout(ContraseñaLayout);
        ContraseñaLayout.setHorizontalGroup(
            ContraseñaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ContraseñaLayout.createSequentialGroup()
                .addComponent(chbSeguridad)
                .addGap(8, 8, 8)
                .addGroup(ContraseñaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ContraseñaLayout.createSequentialGroup()
                        .addComponent(rbtWAP)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtContraseña, javax.swing.GroupLayout.DEFAULT_SIZE, 124, Short.MAX_VALUE))
                    .addComponent(rbtWEP))
                .addContainerGap())
        );
        ContraseñaLayout.setVerticalGroup(
            ContraseñaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ContraseñaLayout.createSequentialGroup()
                .addGroup(ContraseñaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtWEP)
                    .addComponent(chbSeguridad))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(ContraseñaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(rbtWAP)
                    .addComponent(jLabel2)
                    .addComponent(txtContraseña, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(6, 6, 6)
                        .addComponent(cmbEncendido, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtMAC, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(Contraseña, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbEncendido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtMAC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(Contraseña, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(20, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void chbSeguridadMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_chbSeguridadMouseClicked
        if(this.chbSeguridad.isSelected()) {
            this.rbtWAP.setEnabled(true);
            this.rbtWEP.setEnabled(true);
            this.txtContraseña.setEnabled(true);
        } else {
            this.rbtWAP.setEnabled(false);
            this.rbtWEP.setEnabled(false);
            this.txtContraseña.setEnabled(false);
        }
    }//GEN-LAST:event_chbSeguridadMouseClicked

     public static String generarMAC()
    {
        Random r = new Random();
        String mac="";

        for(int i=0; i<12;i++)
        {
            if(i==0)//0 1 8 A
            {
                while(true)
                {
                    int v= r.nextInt(11);
                    String a = getValor(v);
                    if(a.compareTo("0")==0 ||a.compareTo("1")==0 || a.compareTo("8")==0 || a.compareTo("A")==0)
                    {
                        mac+=a;
                        break;
                    }
                }
            }
            else
            {
                if(i==1)//0 2 4 8 A
                {
                    while(true)
                    {
                        int v= r.nextInt(11);
                        String a = getValor(v);
                        if(a.compareTo("0")==0 ||a.compareTo("2")==0 ||a.compareTo("4")==0 || a.compareTo("8")==0 || a.compareTo("A")==0)
                        {
                            mac+=a;
                            break;
                        }
                    }
                }
                else
                {
                    int v= r.nextInt(16);
                    String a = getValor(v);
                    mac+=a;
                }
            }

            if(i%2!=0 && i!=11)
                mac+="-";
        }

        return mac;
    }

       private static String getValor(int pos)
    {
        switch(pos)
        {
                case 0: return "0";
                case 1: return "1";
                case 2: return "2";
                case 3: return "3";
                case 4: return "4";
                case 5: return "5";
                case 6: return "6";
                case 7: return "7";
                case 8: return "8";
                case 9: return "9";
                case 10: return "A";
                case 11: return "B";
                case 12: return "C";
                case 13: return "D";
                case 14: return "E";
                case 15: return "F";
        }
         return "";
    }
    public Wireless getWireless()
    {
        boolean estado;

         if(this.cmbEncendido.getSelectedIndex()==0)
        {
            estado=true;
        }
        else
        {
            estado=false;
        }

        int anchoBanda= 11;

        IP ip= new IP(this.txtIP.getText());

        IP mascara= new IP(this.txtMascara.getText());

        boolean seguridad;
        String contraseña;
        if(this.chbSeguridad.isSelected())
        {
            seguridad=true;
            contraseña=this.txtContraseña.getText();
        }
        else
        {
            seguridad=false;
            contraseña="";
        }

        TiposSeguridad ts;
        if(this.rbtWAP.isSelected())
            ts= TiposSeguridad.WAP;
        else
            ts= TiposSeguridad.WEP;


        MAC mac=null;
        try{
          mac=new MAC(this.txtMAC.getText());
        }
        catch(MACException e)
        {
            //Se debe mostrar un cartel que diga el error
        }
        Wireless w = new Wireless(estado, anchoBanda, ip, mascara, seguridad,ts,contraseña,"",mac);

        return w;
    }

    public IP getIP()
    {
        IP ip=null;

            String cad=this.txtIP.getText();
            if(IP.comprobrarIP(cad))
            {
                    ip = new IP(cad);
            }
            else
            {return null;}

        return ip;
    }

    public IP getMascara()
    {
        try
        {
            IP ip = new IP(this.txtMascara.getText());
            return ip;
        }
        catch(Exception e)
        {}
        return null;
   }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel Contraseña;
    private javax.swing.JCheckBox chbSeguridad;
    private javax.swing.JComboBox cmbEncendido;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JRadioButton jRadioButton3;
    private javax.swing.JRadioButton rbtWAP;
    private javax.swing.JRadioButton rbtWEP;
    private javax.swing.JTextField txtContraseña;
    private javax.swing.JTextField txtIP;
    private javax.swing.JTextField txtMAC;
    private javax.swing.JTextField txtMascara;
    // End of variables declaration//GEN-END:variables

}
